﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

namespace QuangMobile
{
    public partial class ViewProduct : System.Web.UI.Page
    {
        protected static int pID;
        protected static int categoryID;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Request.QueryString["ID"] != null)
            {
                pID = Convert.ToInt32(Request.QueryString["ID"]);
            }
            LoadSanPham();
            LoadSanPhamTuongTu();
        }

        private void LoadSanPhamTuongTu()
        {
            string sSql = "SELECT TOP 4 * FROM dbo.tblWebSanPham WHERE HangID_DM = @categoryID AND ID != @ID";
            UCProductBlock Block = (UCProductBlock)LoadControl("UCProductBlock.ascx");
            Block.Title = "Sản phẩm tương tự";
            List<SqlParameter> arrParams = new List<SqlParameter>();
            arrParams.Add(SqlHelper.CreateParameterObject("@categoryID", SqlDbType.Int, ParameterDirection.Input, categoryID));
            arrParams.Add(SqlHelper.CreateParameterObject("@ID", SqlDbType.Int, ParameterDirection.Input, pID));
            DataTable Pdt = SqlHelper.ExecuteDataTable(CommandType.Text, sSql, arrParams);
            Block.ListProduct = Pdt;
            RelateProduct.Controls.Add(Block);
        }

        protected void LoadSanPham()
        {
            string sql = "Select s.*,d.TenDM from tblWebSanPham s inner join tblWebDanhMuc d On s.HangID_DM = d.ID where s.ID = @ID";
            List<SqlParameter> arrParams = new List<SqlParameter>();
            arrParams.Add(SqlHelper.CreateParameterObject("@ID", SqlDbType.Int, ParameterDirection.Input, pID));
            DataTable dt = SqlHelper.ExecuteDataTable(CommandType.Text, sql, arrParams);
            ProductCategory.Text = dt.Rows[0]["TenDM"].ToString();
            ProductImage.AlternateText = dt.Rows[0]["TenSP"].ToString();
            if (dt.Rows[0]["AnhSP"] == DBNull.Value)
            {
                ProductImage.ImageUrl = "../upload/no-image.png";
            }
            else
            {
                ProductImage.ImageUrl = dt.Rows[0]["AnhSP"].ToString();
            }
            ProductTitle.Text = dt.Rows[0]["TenSP"].ToString();
            ProductPrice.Text = Convert.ToDouble(dt.Rows[0]["GiaSP"]).ToString("0,000");
            ProductSize.Text = dt.Rows[0]["KichThuoc"].ToString();
            ProductWeight.Text = dt.Rows[0]["TrongLuong"].ToString();
            ProductBH.Text = dt.Rows[0]["BaoHanh"].ToString();
            ProductName.Text = dt.Rows[0]["TenSP"].ToString();
            ProductSet.Text = dt.Rows[0]["PhuKien"].ToString();
            ProductInfo.Text = dt.Rows[0]["GhiChu"].ToString();
            categoryID = Convert.ToInt32(dt.Rows[0]["HangID_DM"].ToString());
        }
    }
}
